//
//  Pantalla2.m
//  Grues
//
//  Created by Miquel Viladomat on 17/10/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Pantalla2.h"
#import "Pantalla3.h"
#import "ZonaBlavaAppDelegate.h"



@implementation Pantalla2
@synthesize imageView;
@synthesize imageView2;
@synthesize imageView3;
@synthesize imageView4;


-(IBAction)pushPick{
    iv=1;
    UIImagePickerController *picker=[[UIImagePickerController alloc] init];
    picker.delegate=self;
    picker.sourceType=UIImagePickerControllerSourceTypeCamera;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}
-(IBAction)pushPick2{
    iv=2;
    UIImagePickerController *picker=[[UIImagePickerController alloc] init];
    picker.delegate=self;
    picker.sourceType=UIImagePickerControllerSourceTypeCamera;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}
-(IBAction)pushPick3{
    iv=3;
    UIImagePickerController *picker=[[UIImagePickerController alloc] init];
    picker.delegate=self;
    picker.sourceType=UIImagePickerControllerSourceTypeCamera;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}
-(IBAction)pushPick4{
    iv=4;
    UIImagePickerController *picker=[[UIImagePickerController alloc] init];
    picker.delegate=self;
    picker.sourceType=UIImagePickerControllerSourceTypeCamera;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}
-(IBAction)esborrar{
    /*
    
     */
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"" message:@"Aquesta acció esborrarà totes les fotos i no es pot desfer." delegate:self cancelButtonTitle:@"Conservar" otherButtonTitles:@"Eliminar", nil ];
    
    [message show];
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    
    if([title isEqualToString:@"Eliminar"])
    {
        UIImage* imatgecamera = [UIImage imageNamed:@"iconacamera.png"];
        imageView.image=imatgecamera;
        imageView2.image=imatgecamera;
        imageView3.image=imatgecamera;
        imageView4.image=imatgecamera;
        ZonaBlavaAppDelegate *dataCenter=(ZonaBlavaAppDelegate *)[[UIApplication sharedApplication] delegate];
        dataCenter.foto1v=nil;
        dataCenter.foto2v=nil;
        dataCenter.foto3v=nil;
        dataCenter.foto4v=nil;
    }
    
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:
(NSDictionary *)editingInfo{
    ZonaBlavaAppDelegate *dataCenter=(ZonaBlavaAppDelegate *)[[UIApplication sharedApplication] delegate];
   
    CGSize newSize=CGSizeMake(960,1280);
    if(image.size.width>image.size.height){
        newSize = CGSizeMake(1280,960);
    }
    
    UIGraphicsBeginImageContext(newSize);
   
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    
    UIGraphicsEndImageContext();
    if(iv==1){
        [picker.parentViewController dismissModalViewControllerAnimated:YES];
        imageView.image=newImage;
        dataCenter.foto1v=newImage;
        
    }
    if(iv==2){
        [picker.parentViewController dismissModalViewControllerAnimated:YES];
        imageView2.image=newImage;
        dataCenter.foto2v=newImage;
    }
    if(iv==3){
        [picker.parentViewController dismissModalViewControllerAnimated:YES];
        imageView3.image=newImage;
        dataCenter.foto3v=newImage;
    }
    if(iv==4){
        [picker.parentViewController dismissModalViewControllerAnimated:YES];
        imageView4.image=newImage;
        dataCenter.foto4v=newImage;
    }
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker.parentViewController dismissModalViewControllerAnimated:YES];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

-(void)viewWillAppear:(BOOL)animated{
    
    ZonaBlavaAppDelegate *dataCenter=(ZonaBlavaAppDelegate *)[[UIApplication sharedApplication] delegate];
    UIImage* imatgecamera = [UIImage imageNamed:@"iconacamera.png"];
    if(dataCenter.foto1v==nil){
        imageView.image=imatgecamera;
    }
    if(dataCenter.foto2v==nil){
        imageView2.image=imatgecamera;
    }
    if(dataCenter.foto3v==nil){
        imageView3.image=imatgecamera;
    }
    if(dataCenter.foto4v==nil){
        imageView4.image=imatgecamera;
    }
}
#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
